function [fm dif] = getFGmask(I, mu, vari)
% GETFGMASK computes the foreground mask for image I based on the mean
%   MU and variance VARI of the background model.
% 
% Project:  Foreground detection [Bildfolgen LU]@CVL/TUWIEN
% Author:   Gruppe 10
% Date:     03/2011

% theresholds
T_BS = 16;
T_g = 0.01* T_BS;

% background substraction
dif = abs(double(I)-mu);
d = dif>T_BS;
F_s = d(:,:,1) | d(:,:,2) | d(:,:,3);

% random camera noise
mu_RGB = mean(mean(mu));
cov = var(var(vari));
cov = diag(cov(:)');
y = mvnpdf(reshape(double(I), [], 3), mu_RGB(:)', cov);
F_g = reshape(y, size(I,1), size(I,2));
F_g = F_g < T_g;

% foreground mask
fm = F_s & F_g;
end
